/**
 * 北京中流科技有限公司 版权所有
 * http://www.chinasoa.com.cn
 */
package cn.com.chinasoa.archive.portlet;

import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jcr.LoginException;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.w3c.dom.Element;

import cn.com.chinasoa.archive.portlet.model.Archive;
import cn.com.chinasoa.archive.runtime.ArchiveRuntime;

import com.x4ee.soa.portal.framework.core.BaseRequestFilter;
import com.x4ee.soa.portal.framework.core.FrameworkConfig;
import com.x4ee.soa.portal.framework.core.RenderConfig;
import com.x4ee.soa.portal.framework.core.RenderFilter;

/**
 * 文档列表
 * @author 刘洋(liuyang@chinasoa.com.cn)
 * 2007-7-30下午02:18:19
 */
public class ArchiveListRenderFilter extends BaseRequestFilter implements RenderFilter {

	public String process(RenderRequest renderRequest, RenderResponse renderResponse,
			RenderConfig renderConfig) throws PortletException, IOException {
		
		Map archiveModelMap = null;
		Object archiveModelMapObj = renderRequest.getPortletSession().getAttribute(ArchiveUploadHelper.ARCHIVE_MODEL_MAP);
		if((archiveModelMapObj!=null)&&(archiveModelMapObj instanceof List)){
			archiveModelMap= (Map)archiveModelMapObj;
		}
		if(archiveModelMap==null){
			archiveModelMap = new HashMap();
			Repository tRepository = ArchiveRuntime.getRepository(renderRequest);
	        Session session=null;
			try {
				session = tRepository.login(new SimpleCredentials(ArchiveUploadHelper.SEP,ArchiveUploadHelper.X4EE.toCharArray()));
	            Node root = session.getRootNode();

	            for(NodeIterator nIt = root.getNodes();nIt.hasNext();){
	            	Node archiveNode = (Node)nIt.next();
	            	String archiveNodeName = archiveNode.getName();
	            	if((archiveNodeName!=null)&&(archiveNodeName.startsWith(ArchiveUploadHelper.ARCHIVE_PREX))){
			            Property archiveNamePty = archiveNode.getProperty(ArchiveUploadHelper.ARCHIVE_NAME);
			            Property archiveTitlePty = archiveNode.getProperty(ArchiveUploadHelper.ARCHIVE_TITLE);
			            Property archiveUserPty = archiveNode.getProperty(ArchiveUploadHelper.ARCHIVE_USER);
			            Property archiveCalenderPty = archiveNode.getProperty(ArchiveUploadHelper.ARCHIVE_CALENDAR);
			            
			            String archiveName = archiveNamePty.getValue().getString();
			            String archiveTitle = archiveTitlePty.getValue().getString();
			            String archiveUser = archiveUserPty.getValue().getString();
			            Calendar archiveCalender = archiveCalenderPty.getValue().getDate();
			            
			            Archive tArchive = new Archive();
			            tArchive.setName(archiveName);
			            tArchive.setNodeName(archiveNodeName);
			            tArchive.setTitle(archiveTitle);
			            tArchive.setUser(archiveUser);
			            tArchive.setCalendar(archiveCalender);
			            
			            archiveModelMap.put(archiveNodeName,tArchive);
	            	}
	            }
			} catch (LoginException e) {
				e.printStackTrace();
			} catch (RepositoryException e) {
				e.printStackTrace();
			} finally {
				if(session!=null)session.logout();
	        }			
			renderRequest.getPortletSession().setAttribute(ArchiveUploadHelper.ARCHIVE_MODEL_MAP,archiveModelMap); 
		}
		return ArchiveUploadHelper.DEFAULT;
	}

	public void init(Element element, FrameworkConfig frameworkConfig) throws PortletException {
		super.init(element, frameworkConfig);
		ArchiveRuntime.createRepository(frameworkConfig);
	}

}
